﻿using MiniErp.Model.Entities.AssetManagement;
using MiniErp.Model.Templates;
using MiniErp.Repository.IRepository.IAssetManagement;
using MiniErp.Service.DBService.IService.IAssetManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniErp.Service.DBService.Service.AssetManagement
{
    public class AssetIndexService : IAssetIndexService
    {
        private readonly IAssetInfoRepository _air;
        public AssetIndexService(IAssetInfoRepository air)
        {
            _air = air;
        }
        public async Task<int> AddAssetInfoAsync(AssetInfoEntity userinfo)
        {
            return await _air.AddAssetInfoAsync(userinfo);
        }

        public async Task<List<AssetInfoEntity>> GetAllAssetInfoAsync(int Page, int num)
        {
            return await _air.GetAllAssetInfoAsync(Page, num);
        }

        public async Task<MessageTemplate<List<AssetInfoEntity>>> GetFuzzyAssetInfoAsync(string value, int Page, int num)
        {
            var message = new MessageTemplate<List<AssetInfoEntity>>();
            if (Page == 0 || num == 0 || string.IsNullOrWhiteSpace(value))
            {
                message.Status = 403;
                message.Message = $"提交数据不合法";
            }
            try
            {
                var result = await _air.GetFuzzyAssetInfoAsync(value, Page, num);
                if (result.Count == 0)
                {
                    message.Status = 403;
                    message.Message = $"未找到相关数据";
                }
                else if (result.Count > num)
                {
                    message.Status = 403;
                    message.Message = $"出现未知异常导致分页失败";
                }
                else
                {
                    message.Status = 200;
                    message.Message = $"请求成功";
                    message.data.Add(result);
                }
            }
            catch (Exception ex)
            {
                message.Status = 500;
                message.Message = $"服务器异常,异常信息为:{ex.Message}";
                message.data = null;
            }
            return message;

        }
    }
}
